// import { ActionSheet } from 'native-base'
import ImagePicker from 'react-native-image-picker'
import API from '../../api/request'

const defaultOptions = {
  title: '选择图片',
  cancelButtonTitle: '取消',
  takePhotoButtonTitle: '拍照',
  chooseFromLibraryButtonTitle: '选择照片',
  // customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
  cameraType: 'back',
  mediaType: 'photo',
  videoQuality: 'high',
  durationLimit: 10,
  maxWidth: 300,
  maxHeight: 300,
  quality: 0.8,
  angle: 0,
  allowsEditing: false,
  noData: false,
  storageOptions: {
    skipBackup: true
  }
}
export default function selectPhotoTapped (opts) {
  let options = { ...defaultOptions, ...opts }
  ImagePicker.showImagePicker(options, async response => {
    console.log('Response = ', response)

    if (response.didCancel) {
      console.log('User cancelled photo picker')
    } else if (response.error) {
      console.log('ImagePicker Error: ', response.error)
    } else if (response.customButton) {
      console.log('User tapped custom button: ', response.customButton)
    } else {
      let _onSelect = options._onSelect
      // let source = 'data:image/jpeg;base64,' + response.data
      let source = response.data
      // let source = {
      //   path: response.uri,
      //   uri: response.uri,
      //   type: response.type,
      //   name: response.fileName
      // }
      // let source = { content: response.data }
      // let formData = new FormData()
      // formData.append('file', source)
      // let imgurl = await uploadApi(formData)
      let imgurl = await uploadApi({
        fileName: response.fileName,
        content: source
      })
      _onSelect && _onSelect(imgurl)
      // You can also display the image using data:
      // let source = { uri: 'data:image/jpeg;base64,' + response.data };
    }
  })
}

// 上传图片接口
async function uploadApi (formData) {
  // const url = '/api/external/upload'
  const url = '/api/external/upload-by-byte'
  let res = await API.post(url, formData)
  if (res && res.code === '200') {
    return res.data
  }

  return false
}
